package com.hyx.api.controller.applet_comm;

import com.hyx.api.config.ApiPrefixRestController;
import com.hyx.api.domain.User;
import com.hyx.api.domain.Wallet;
import com.hyx.api.domain.bo.WalletChangeInfoBo;
import com.hyx.api.domain.vo.WalletChangeInfoVo;
import com.hyx.api.service.pc_admin.IWalletChangeInfoService;
import com.hyx.api.service.pc_admin.IWalletService;
import com.hyx.api.utils.base.MobileBaseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@ApiPrefixRestController
@RequestMapping("/wallet")
public class AppWalletController extends BaseController {

    private final IWalletService walletService;

    private final MobileBaseService mobileBaseService;

    private final IWalletChangeInfoService walletChangeInfoService;

    /**
     * 获取当前登录用户钱包详情
     */
    @GetMapping("/detail")
    public AjaxResult walletDetail() {
        User user = mobileBaseService.hasLogin();
        Map<String, Object> map = new HashMap<>();
        map.put("wallet", walletService.selectWalletByCompanyOrRecovery(user.getCompanyId(), user.getRecoveryId()));
        map.put("expend", walletChangeInfoService.nowDayExpend());
        return AjaxResult.success(map);
    }

    /**
     * 收支记录表
     *
     * @param bo
     * @return
     */
    @GetMapping("/changeInfo/list")
    public TableDataInfo changeInfoList(WalletChangeInfoBo bo) {
        User user = mobileBaseService.hasLogin();
        Wallet wallet = walletService.selectWalletByCompanyOrRecovery(user.getCompanyId(), user.getRecoveryId());
        if (ObjectUtils.isEmpty(wallet))
            throw new RuntimeException("没有钱包信息");
        bo.setWalletId(wallet.getId());
        startPage();
        List<WalletChangeInfoVo> list = walletChangeInfoService.selectWalletChangeInfoList(bo);
        return getDataTable(list);
    }
}
